/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package photoshare.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import photoshare.dto.AccountDTO;
import photoshare.util.DBUtil;

/**
 *
 * @author PhotoShare
 */
public class AccountDAO {

    private String READ_BY_ALL = "SELECT * FROM ACCOUNTS";
    private String READ_BY_ID = "SELECT * FROM ACCOUNTS WHERE AccountID = ?";
    private String READ_BY_IS_ACTIVE = "SELECT * FROM ACCOUNTS WHERE IsACTIVE = ?";
    private String CREATE = "INSERT INTO ACCOUNTS VALUES(?,?,?,?,?,?,?,?,?)";
    private String DELETE_BY_ID = "UPDATE ACCOUNTS SET IsACTIVE= 0 WHERE AccountID = ?";
    private String UPDATE_BY_ID = "UPDATE ACCOUNTS SET PASSWORD = ? WHERE AccountID = ?";
    private String CHECK_LOGIN = "SELECT * FROM ACCOUNTS WHERE USERNAME = ? AND PASSWORD = ?";

    private Connection conn = null;
    private PreparedStatement pst = null;

    public AccountDAO() {
    }
    /**
     *
     * @This method use to insert new entity of account to database
     */
    public boolean create(AccountDTO accDTO) throws SQLException {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(CREATE);
            pst.setString(1, accDTO.getUsername());
            pst.setString(2, accDTO.getPassword());
            pst.setBoolean(3, accDTO.getIsActive());
            pst.setString(4, accDTO.getCodeActive());
            pst.setString(5, accDTO.getFullname());
            pst.setString(6, accDTO.getBirthday());
            pst.setString(7, accDTO.getAddress());
            pst.setString(8, accDTO.getEmail());
            pst.setString(9, accDTO.getDateCreate());
            int num = 0;
            num = pst.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            return false;
        } finally {
            DBUtil.closeConn(conn);
            pst.close();
        }
    }

    public ArrayList<AccountDTO> readAll() {
         try {
            ArrayList<AccountDTO> arr = null;
            arr = new ArrayList<AccountDTO>();
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(READ_BY_ALL);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                AccountDTO dto = new AccountDTO();
                dto.setUsername(rs.getString(2));
                dto.setPassword(rs.getString(3));
                dto.setIsActive(rs.getBoolean(4));
                dto.setCodeActive(rs.getString(5));
                dto.setFullname(rs.getString(6));
                dto.setAvatar(rs.getString(7));
                dto.setEntry(rs.getString(8));
                dto.setBirthday(rs.getString(9));
                dto.setAddress(rs.getString(10));
                dto.setEmail(rs.getString(11));
                dto.setDateCreate(rs.getString(12));
                arr.add(dto);
            }
            return arr;
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            DBUtil.closeConn(conn);
        }
    }

    public AccountDTO readById(Integer accountId) {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(READ_BY_ID);
            pst.setInt(1, accountId);
            //pst = conn.prepareStatement("select * from acc WHERE accountID = 'admin'");
            ResultSet rs = pst.executeQuery();
            AccountDTO accDTO = new AccountDTO();
            while (rs.next()) {
                accDTO.setUsername(rs.getString(2));
                accDTO.setPassword(rs.getString(3));
                accDTO.setIsActive(rs.getBoolean(4));
                accDTO.setCodeActive(rs.getString(5));
                accDTO.setFullname(rs.getString(6));
                accDTO.setBirthday(rs.getString(7));
                accDTO.setAddress(rs.getString(8));
                accDTO.setEmail(rs.getString(9));
                accDTO.setDateCreate(rs.getString(10));
            }
            return accDTO;
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            DBUtil.closeConn(conn);
        }
    }

    public ArrayList<AccountDTO> readByIsActive(boolean isActive) {
        try {
            ArrayList<AccountDTO> arr = null;
            arr = new ArrayList<AccountDTO>();
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(READ_BY_IS_ACTIVE);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                AccountDTO dto = new AccountDTO();
                dto.setUsername(rs.getString(2));
                dto.setPassword(rs.getString(3));
                dto.setIsActive(rs.getBoolean(4));
                dto.setCodeActive(rs.getString(5));
                dto.setFullname(rs.getString(6));
                dto.setBirthday(rs.getString(7));
                dto.setAddress(rs.getString(8));
                dto.setEmail(rs.getString(9));
                dto.setDateCreate(rs.getString(10));
                arr.add(dto);
            }
            return arr;
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            DBUtil.closeConn(conn);
        }
    }

    public boolean updateById(AccountDTO accountDTO, Integer accountId) throws SQLException {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(UPDATE_BY_ID);
            //Tham so can Update trong cau lenh SQL
            pst.setString(1, accountDTO.getPassword());
            //Tham so dieu kien cua cau lenh Update
            pst.setInt(2, accountId);
            int num = 0;
            num = pst.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return false;
        } finally {
            DBUtil.closeConn(conn);
            pst.close();
        }
    }
    
    public boolean deleteById(Integer accountId) throws SQLException {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(DELETE_BY_ID);            
            //Tham so dieu kien cho cau lenh Update
            pst.setInt(1, accountId);
            int num = 0;
            num = pst.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            return false;
        } finally {
            DBUtil.closeConn(conn);
            pst.close();
        }
    }

    public boolean login(String username, String password) {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(CHECK_LOGIN);
            pst.setString(1, username);
            pst.setString(2, password);
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            return false;
        } finally {
            //Dong ca ResultSet va PreparedStatement
            DBUtil.closeConn(conn);
        }

    }

    public boolean changePassword(Integer accountId, String currentPassword, String newPassword) throws SQLException{
        AccountDTO accountDTO = new AccountDTO();
        accountDTO = this.readById(accountId);
        if(currentPassword.equals(accountDTO.getPassword())){
            accountDTO.setPassword(newPassword);
            this.updateById(accountDTO, accountId);
            return true;
        }
        return false;
    }
    
}


   

   
